<template>
  <div class="ever-note-wrap">
    <div class="handle-wrap">
      <h4>我的笔记</h4>
      <div>
        <el-link :underline="false" type="primary" @click="writeNotes"
          >写笔记</el-link
        >
      </div>
    </div>
    <div class="divider"></div>
    <div class="main-notes">
      <el-table :data="noteList" style="width: 100%">
        <el-table-column prop="id" label="id"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template v-slot="scope">
            <el-button @click.stop="editor(scope.row)" type="primary" size="small">
              编辑
            </el-button>

            <el-button @click.stop="remove(scope.row)" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
      <el-pagination
        background
        @current-change="currentChange"
        :page-size="pagination.pageSize"
        layout="prev, pager, next"
        :total="pagination.total"
      />
    </div>
    </div>
  </div>
</template>

<script>
import request from "utils/request";
import { ElMessage, ElMessageBox } from "element-plus";
export default {
  name: "JddChromePluginsEverNote",

  data() {
    return {
      noteList: [],
      pagination: {
        currentPage: 1,
        pageSize: 5,
        total: 0,
      },
    };
  },

  mounted() {
    this.getNoteList();
  },

  methods: {
    writeNotes() {
      this.$router.push({
        path: "/write-notes",
      });
    },
    async getNoteList() {
      let ret = await request.get("/selfNoteList", {
        currentPage: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
      });
      if (ret.code == 200 && ret.data.noteList) {
        this.noteList = ret.data.noteList || [];
        this.pagination.total = ret.data.totalNum;
      }
    },
    async remove(note) {
      ElMessageBox.confirm("确认需要删除该笔记?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let ret = await request.post("/deleteNote", {
            id: note.id,
          });
          if (ret.code == 200) {
            this.$message({
              message: "移除成功",
              type: "success",
            });
            this.getNoteList();
          } else {
            this.$message({
              message: ret.message,
              type: "error",
            });
          }
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消删除",
          });
        });
    },
    async editor(note) {
      this.$router.push({
        path: "/write-notes",
        query: {
          id: note.id,
        },
      });
    },
    async currentChange(pageSize) {
      this.pagination.currentPage = pageSize;
      this.getNoteList();
    },
  },
};
</script>

<style lang="scss" scoped>
.ever-note-wrap {
  .handle-wrap {
    display: flex;
    line-height: 34px;
    justify-content: space-between;
    padding: 0 12px;
  }
  .divider {
    width: 100%;
    height: 1px;
    background: #dcdfe6;
  }
  .main-notes {
    padding: 10px 0px;
  }
  .notes {
    padding: 12px 15px;
    margin: 8px 0px;
    box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
    border-radius: 5px;
    cursor: pointer;
    .header {
      line-height: 30px;
    }
    .footer {
      display: flex;
      justify-content: flex-end;
    }
  }
  .no-data {
    text-align: center;
    padding: 80px 0px 10px;
    .no-data-footer-text {
      line-height: 32px;
      font-size: 13px;
      margin-top: 5px;
      color: #aeaeae;
    }
  }
  .pagination-container {
    padding: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
